const { File } = require('node-fetch')
const axios = require('axios')

// 获取qq头像
function getQQAvatar(qq) {
    return `https://q1.qlogo.cn/g?b=qq&s=0&nk=${qq}`
}

// url转file
function urlToFile(url) {
    return new Promise(async (resolve, reject) => {
        try {
            const res = await axios({
                url: url,
                method: 'get',
                responseType: 'arraybuffer',
            })
            const arrayBuffer = res.data
            const buffer = Buffer.from(arrayBuffer)
            const id = Date.now()
            const file = new File([buffer], `${id}.jpg`, { type: 'image/jpeg' })
            resolve(file)
        } catch (err) {
            console.log(err)
            reject(err)
        }
    })
}

function blobToBase64(blob) {
    return new Promise(async (resolve, reject) => {
        try {
            const arrayBuffer = await blob.arrayBuffer()
            const base64 = Buffer.from(arrayBuffer).toString('base64')
            // resolve(`base64://${base64}`)
            resolve(base64)
        } catch (err) {
            reject(err)
        }
    })
}

module.exports = {
    getQQAvatar,
    urlToFile,
    blobToBase64,
}
